home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / IBTip / Source / Tty.m < prev   
Encoding:
Text File  |  1995-06-12  |  5.5 KB  |  258 lines

  1. #import "Tty.h"
  2. #import <stdio.h>
  3. #import <sys/file.h>
  4. #import <string.h>
  5. #import <libc.h>
  6.  
  7. @implementation Tty
  8.   
  9. + newDevice:(char *)devname access:(int)flags
  10. {
  11.   int cc;
  12.  
  13.   self = [super new];
  14.   fd = open(devname, flags, 0);
  15.   if (fd<0) {
  16.     fprintf(stderr,"Couldn't open %s\n",devname); perror("Unix error follows");
  17.     [super free];
  18.     return nil;
  19.   }
  20.   cc=ioctl(fd,TIOCGETP,&origState);
  21.   currentState = origState;
  22.   return self;
  23. }
  24.  
  25.  
  26. + newReadDevice:(char *)devname
  27. {
  28.   return [self newDevice:devname access:O_RDONLY];
  29. }
  30.  
  31. + newWriteDevice:(char *)devname
  32. {
  33.   return [self newDevice:devname access:O_WRONLY];
  34. }
  35.  
  36. + newReadWriteDevice:(char *)devname
  37. {
  38.   return [self newDevice:devname access:O_RDWR];
  39. }
  40.  
  41. + newExistingfd:(int)thisfd  // for already existing file descriptor (eg. 0=stdin 1=stdout 2=stderr)
  42. {
  43.   int cc;
  44.  
  45.   self = [super new];
  46.   fd = thisfd;
  47.   cc=ioctl(fd,TIOCGETP,&origState);
  48.   currentState = origState;
  49.   return self;
  50. }
  51.  
  52. - reset
  53. {
  54.   currentState = origState;
  55.   [self activateState];
  56.   return self;
  57. }
  58.  
  59. - setEcho
  60. {
  61.   currentState.sg_flags |= ECHO;
  62.   [self activateState];
  63.   return self;
  64. }
  65.  
  66. - unSetEcho
  67. {
  68.   currentState.sg_flags &= (~ECHO);
  69.   [self activateState];
  70.   return self;
  71. }
  72.  
  73. - setRaw
  74. {
  75.   currentState.sg_flags |= RAW;
  76.   [self activateState];
  77.   return self;
  78. }
  79.  
  80. - unSetRaw
  81. {
  82.   currentState.sg_flags &= (~RAW);
  83.   [self activateState];
  84.   return self;
  85. }
  86.  
  87. - setCbreak
  88. {
  89.   currentState.sg_flags |= CBREAK;
  90.   [self activateState];
  91.   return self;
  92. }
  93.  
  94. - unSetCbreak
  95. {
  96.   currentState.sg_flags &= (~CBREAK);
  97.   [self activateState];
  98.   return self;
  99. }
  100.  
  101. - setCrmod
  102. {
  103.   currentState.sg_flags |= CRMOD;
  104.   [self activateState];
  105.   return self;
  106. }
  107.  
  108. - unSetCrmod
  109. {
  110.   currentState.sg_flags &= (~CRMOD);
  111.   [self activateState];
  112.   return self;
  113. }
  114.  
  115. - activateState
  116. {
  117.   int cc;
  118.   cc=ioctl(fd,TIOCSETP,¤tState);
  119.   return self;
  120. }
  121.  
  122.  
  123. static struct setbps{
  124.   int speed;
  125.   int speedCode;
  126. } speeds[] = {{0,B0},{50,B50},{75,B75},{110,B110},
  127.         {134,B134},{150,B150},{200,B200},
  128.         {300,B300},{600,B600},{1200,B1200},
  129.         {1800,B1800},{2400,B2400},
  130.         {4800,B4800},{9600,B9600}};
  131. - setSpeed:(int)bps
  132. {
  133.   int i;
  134.   for (i=0; i<sizeof(speeds)/sizeof(struct setbps); i++) {
  135.     if (bps==speeds[i].speed) {
  136.       currentState.sg_ispeed = speeds[i].speedCode;
  137.       currentState.sg_ospeed = speeds[i].speedCode;
  138.       return [self activateState];
  139.     }
  140.   }
  141.   /* if get to here, requested speed was illegal */
  142.   fprintf(stderr,"Tty method setSpeed: illegal speed %d\n",bps);
  143.   return self;
  144. }
  145.  
  146. - (int) getSpeed
  147. {
  148.   int i, sp;
  149.   sp = currentState.sg_ospeed;
  150.   for (i=0; i<sizeof(speeds)/sizeof(struct setbps); i++) {
  151.     if (sp==speeds[i].speedCode)
  152.       return speeds[i].speed;
  153.   }
  154.   fprintf(stderr,"Tty method getSpeed: currentState.sp_ospeed = %d unknown\n",sp);
  155.   return 0;
  156. }
  157.  
  158. - setParity:(int)parity
  159. {
  160.   par=parity;
  161.   switch (parity) {
  162.   case EVENP:
  163.     currentState.sg_flags |= EVENP;
  164.     currentState.sg_flags &= (~ODDP);
  165.     break;
  166.   case ODDP:
  167.     currentState.sg_flags |= ODDP;
  168.     currentState.sg_flags &= (~EVENP);
  169.     break;
  170.   case NOPARITY:
  171.     currentState.sg_flags |= EVENP;
  172.     currentState.sg_flags |= ODDP;
  173.     break;
  174.   default:
  175.     fprintf(stderr,"Tty method setParity: unrecognized parity:%d\n",parity);
  176.     fprintf(stderr,"should be EVENP=%d, ODDP=%d, or NOPARITY=%d\n",EVENP,ODDP,NOPARITY);
  177.     par=NOPARITY;
  178.   }
  179.   return [self activateState];
  180. }
  181.  
  182. - (int) getParity
  183. {
  184.   return par;
  185. }
  186.  
  187. - (int) getfd
  188. {
  189.   return fd;
  190. }
  191.  
  192. - (int) queuedChars
  193. {
  194.   int numchars;
  195.   ioctl(fd,FIONREAD,&numchars);
  196.   return numchars;
  197. }
  198.  
  199. - (int) readInto:(char *)buf
  200. {
  201.   int i;
  202.   i = read(fd,buf,[self queuedChars]);
  203.   buf[i]=0;
  204.   return (i);
  205. }
  206.  
  207. - (int) writeOut:(char *)buf
  208. {
  209.   int i, len=strlen(buf);
  210.   char *table;
  211.   static char oddtab[128] = {
  212.     0x80, 0x01, 0x02, 0x83, 0x04, 0x85, 0x86, 0x07, 
  213.     0x08, 0x89, 0x8a, 0x0b, 0x8c, 0x0d, 0x0e, 0x8f, 
  214.     0x10, 0x91, 0x92, 0x13, 0x94, 0x15, 0x16, 0x97, 
  215.     0x98, 0x19, 0x1a, 0x9b, 0x1c, 0x9d, 0x9e, 0x1f, 
  216.     0x20, 0xa1, 0xa2, 0x23, 0xa4, 0x25, 0x26, 0xa7, 
  217.     0xa8, 0x29, 0x2a, 0xab, 0x2c, 0xad, 0xae, 0x2f, 
  218.     0xb0, 0x31, 0x32, 0xb3, 0x34, 0xb5, 0xb6, 0x37, 
  219.     0x38, 0xb9, 0xba, 0x3b, 0xbc, 0x3d, 0x3e, 0xbf, 
  220.     0x40, 0xc1, 0xc2, 0x43, 0xc4, 0x45, 0x46, 0xc7, 
  221.     0xc8, 0x49, 0x4a, 0xcb, 0x4c, 0xcd, 0xce, 0x4f, 
  222.     0xd0, 0x51, 0x52, 0xd3, 0x54, 0xd5, 0xd6, 0x57, 
  223.     0x58, 0xd9, 0xda, 0x5b, 0xdc, 0x5d, 0x5e, 0xdf, 
  224.     0xe0, 0x61, 0x62, 0xe3, 0x64, 0xe5, 0xe6, 0x67, 
  225.     0x68, 0xe9, 0xea, 0x6b, 0xec, 0x6d, 0x6e, 0xef, 
  226.     0x70, 0xf1, 0xf2, 0x73, 0xf4, 0x75, 0x76, 0xf7, 
  227.     0xf8, 0x79, 0x7a, 0xfb, 0x7c, 0xfd, 0xfe, 0x7f};
  228.   static char eventab[128] = {
  229.     0x00, 0x81, 0x82, 0x03, 0x84, 0x05, 0x06, 0x87, 
  230.     0x88, 0x09, 0x0a, 0x8b, 0x0c, 0x8d, 0x8e, 0x0f, 
  231.     0x90, 0x11, 0x12, 0x93, 0x14, 0x95, 0x96, 0x17, 
  232.     0x18, 0x99, 0x9a, 0x1b, 0x9c, 0x1d, 0x1e, 0x9f, 
  233.     0xa0, 0x21, 0x22, 0xa3, 0x24, 0xa5, 0xa6, 0x27, 
  234.     0x28, 0xa9, 0xaa, 0x2b, 0xac, 0x2d, 0x2e, 0xaf, 
  235.     0x30, 0xb1, 0xb2, 0x33, 0xb4, 0x35, 0x36, 0xb7, 
  236.     0xb8, 0x39, 0x3a, 0xbb, 0x3c, 0xbd, 0xbe, 0x3f, 
  237.     0xc0, 0x41, 0x42, 0xc3, 0x44, 0xc5, 0xc6, 0x47, 
  238.     0x48, 0xc9, 0xca, 0x4b, 0xcc, 0x4d, 0x4e, 0xcf, 
  239.     0x50, 0xd1, 0xd2, 0x53, 0xd4, 0x55, 0x56, 0xd7, 
  240.     0xd8, 0x59, 0x5a, 0xdb, 0x5c, 0xdd, 0xde, 0x5f, 
  241.     0x60, 0xe1, 0xe2, 0x63, 0xe4, 0x65, 0x66, 0xe7, 
  242.     0xe8, 0x69, 0x6a, 0xeb, 0x6c, 0xed, 0xee, 0x6f, 
  243.     0xf0, 0x71, 0x72, 0xf3, 0x74, 0xf5, 0xf6, 0x77, 
  244.     0x78, 0xf9, 0xfa, 0x7b, 0xfc, 0x7d, 0x7e, 0xff};
  245.   if (par==NOPARITY) {
  246.     return write(fd,buf,len);
  247.   }
  248.   else
  249.     table=(par==EVENP)? eventab : oddtab;
  250.   for (i=0; i<len; i++) {
  251.     if (write(fd, table+(*buf++), 1)<0)
  252.       return -1;
  253.   }
  254.   return len;
  255. }
  256.  
  257. @end
  258.